package cn.likole.oj.module;

import cn.likole.oj.bean.Achievement;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.plugins.apidoc.annotation.Api;

import javax.servlet.http.HttpSession;
import java.util.List;

import static cn.likole.oj.constant.Constant.ACHIEVEMENT_DESCRIPTION;
import static cn.likole.oj.constant.Constant.ACHIEVEMENT_NAME;

/**
 * Created by likole on 8/1/18.
 */
@At("/achievement")
@IocBean
@Api(name = "成就模块")
@Filters(@By(type = CheckSession.class, args = {"uid", "/"}))
public class AchievementModule {

    @Inject
    private Dao dao;


    @At
    @Api(name = "成就列表",description = "uid为空时,获取自己的成就列表")
    public Object achievements(@Param("uid")int uid,HttpSession session){
        NutMap re=new NutMap();
        List<Achievement> achievements;
        if(uid!=0&&uid!=(int)session.getAttribute("uid")){
            achievements= dao.query(Achievement.class, Cnd.where("userId","=",uid).and("showoff","=",true));
        }else{
            achievements= dao.query(Achievement.class, Cnd.where("userId","=",session.getAttribute("uid")));
        }
        return re.setv("ok",true).setv("achievements",achievements).setv("name",ACHIEVEMENT_NAME).setv("description",ACHIEVEMENT_DESCRIPTION);
    }

    @At
    @Api(name = "是否公开",description = "决定该achievement是否在资料页里对他人公开显示")
    public Object showoff(@Param("achievementId") int achievementId,@Param("public")boolean what,HttpSession session){
        NutMap re=new NutMap();
        Achievement achievement= dao.fetchx(Achievement.class,achievementId,(int)session.getAttribute("uid"));
        if(achievement==null) return re.setv("ok",what).setv("msg","你无此成就");
        achievement.setShowoff(true);
        return re.setv("ok",true);
    }

}
